//logs.js
const App = getApp();
import { hexMD5 } from "../../utils/md5.js"
Page({
  data: {
    tel: '15980829676',
    password: '123456'
    // real_name: '',
    // password: ''
  },

  onLoad(){
    // 如果存在 token 直接去首页
    if (wx.getStorageSync('token')) {
      wx.switchTab({
        url: '/pages/index/index',
      })
    }
  },
  // 自定义方法
  goUrl (e) {
    let url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url,
    })
  },
  bindKeyInput(e) {
    let key = e.currentTarget.dataset.key;
    let date = {};
    date[key] = e.detail.value;
    this.setData(date);
  },
  goLog() {
    if (!this.data.tel || !this.data.password) {
      App.prompts('手机号或密码不能为空');
      return false;
    }
    let url = 'mp/user/login';
    App.ajax(url, 'POST', { tel: this.data.tel, password: hexMD5(this.data.password)}).then(res => {
      console.log('请求对了额', res)
      if (res.status === 10000) {
        // 存token
        wx.setStorage({
          key:"token",
          data: res.data.token
        });
        wx.switchTab({
          url: '/pages/index/index'
        });
      } else {
        App.prompts(res.msg)
      }
    }).catch(err => {
      console.log('报错了', err)
    })
  },
})
